<?php
/**
 * Created by PhpStorm.
 * User: vanity
 * Date: 2018/1/5
 * Time: 13:11
 */
namespace Admin\Model;
use Think\Model;
class PermissionRoleModel extends Model
{
    protected $tableName = 'permission_role';

    public function setPermission($role_id,$array)
    {
        if(empty($array)){
            return true;
        }
        $map = array('role_id'=>$role_id);
        $old = $this->where($map)->getField('permission_id',true);
        if(empty($old)){
            $AddData = $array;
        }else{
            $AddData = array_diff($array,$old);
            $DelData = array_diff($old,$array);
            if(!empty($DelData)){
                $map['permission_id'] = array('in',$DelData);
                $this->where($map)->delete();
            }
        }
        foreach($AddData as $v){
            $dataList[] = array('permission_id'=>$v,'role_id'=>$role_id);
        }
        $this->addAll($dataList);
    }
}